package com.github.m6d21.sso.service.user.impl;

import static com.github.m6d21.sso.support.enums.StatusCode.REQUEST_PARAM_ERROR;

import com.github.m6d21.sso.core.exception.BusinessException;
import com.github.m6d21.sso.core.exception.ParamException;
import com.github.m6d21.sso.dao.user.dao.IUserDao;
import com.github.m6d21.sso.model.user.entity.User;
import com.github.m6d21.sso.model.user.qo.UserQO;
import com.github.m6d21.sso.service.user.IUserService;
import com.github.m6d21.sso.support.model.SsoTokenWrapper;
import com.github.m6d21.sso.support.session.SsoSessionHolder;
import com.github.m6d21.sso.support.token.SsoTokenHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 *<p>
 *UserServiceImpl
 *</p>
 *
 * @author liuyang
 * @date 2018-12-05 Wed 22:39:07
 * @since 1.0.0
 */
@Service("userService")
public class UserServiceImpl implements IUserService {

    private static final int DEFAULT_PAGE_NO = 1;
    private static final int DEFAULT_PAGE_SIZE = 20;
    @Resource
    private IUserDao userDao;

    @Override
    public List<User> findByUsernameAndPassword(String username, String password) {
        return userDao.selectUsernamePassword(username, password);
    }

    @Override
    public List<User> query(UserQO userQO) {
        return userDao.query(userQO);
    }

    @Override
    public PageInfo<User> queryByPage(Integer pageNo, Integer pageSize, UserQO userQO) {
        pageNo = (pageNo == null ? DEFAULT_PAGE_NO : pageNo);
        pageSize = (pageSize == null ? DEFAULT_PAGE_SIZE : pageSize);
        PageHelper.startPage(pageNo, pageSize);
        List<User> list = userDao.query(userQO);
        return PageInfo.of(list);
    }

    @Override
    public User saveUser(User user) {
        boolean success = userDao.insertSelectiveUser(user);
        if (!success) {
            throw new BusinessException("保存用户失败");
        }
        return user;
    }

    @Override
    public User modifyUserById(User user) {
        boolean success = userDao.updateSelectiveUserById(user);
        if (!success) {
            throw new BusinessException("修改用户信息失败");
        }
        return user;
    }

    @Override
    public User getUserByToken(String token) {
        if (StringUtils.isEmpty(token)) {
            throw new ParamException(REQUEST_PARAM_ERROR.getCode(), "token不能为空");
        }

        User user = null;
        SsoTokenWrapper ssoTokenWrapper = SsoTokenHelper.verifyToken(token);
        if (ssoTokenWrapper != null && StringUtils.isNotEmpty(ssoTokenWrapper.getUid())) {
            Integer uid = Integer.valueOf(ssoTokenWrapper.getUid());
            user = userDao.getById(uid);
        }
        return user;
    }

    @Override
    public User getCurrentUser() {
        return userDao.getById(SsoSessionHolder.getUid());
    }

    @Override
    public Boolean exist(String username) {
        List<User> list = userDao.selectUsername(username);
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        return true;
    }

    @Override
    public User loadUserByUsername(String username) {
        List<User> list = userDao.selectUsername(username);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else if (list.size() == 1) {
            return list.get(0);
        } else {
            throw new BusinessException("查询出多个用户, username: " + username);
        }
    }

    @Override
    public Boolean deleteUserById(Integer id) {
        if (id == null) {
            return false;
        }
        return userDao.deleteById(id);
    }

    @Override
    public Boolean deleteUserByIds(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return userDao.deleteByIds(ids);
    }
}
